package com.vito.dcs.process.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.DataGrid;
import com.vito.base.model.Result;
import com.vito.base.tools.I18nResMsg;
import com.vito.base.util.Validata;
import com.vito.dcs.process.model.PavingProcessModel;
import com.vito.dcs.process.service.PavingProcessService;

@Controller
@RequestMapping("/dcs/process")
public class PavingProcessAction extends BaseAction{

	private static final Logger log = LoggerFactory.getLogger(PavingProcessAction.class);
	
	
	private static volatile String id = "";
	
	@Reference
	private PavingProcessService pavingProcessService;
	
	@RequestMapping("/savePavingProcessModel")
	@ResponseBody
	public Result savePavingProcessModel(PavingProcessModel processModel){
		Result res = new Result();
		UserModel obj = SessionMap.getObj();
		
		if(obj == null || Validata.isNullOrEmpty(obj.getUserId())){
			res.setCode(Result.CODE_LOGIN_REQUIRE);
			res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
			return res;
		}
		
		if(id.equals(processModel.getPageTokenId())){
			res.setCode(Result.CODE_OK);
			return res;
		}
		
		try {
			processModel.setUpdateUser(obj.getUserId());
			id = processModel.getPageTokenId();
			pavingProcessService.savePavingProcessModel(processModel);
			res.setCode(Result.CODE_OK);
			res.setMsg(I18nResMsg.SAVESUC());
		} catch (Exception e) {
			res.setCode(Result.CODE_ERROR);
			res.setMsg(I18nResMsg.SAVEERR());
			log.error(I18nResMsg.SAVEERR(), e);
		}
		return res;
	}
	@RequestMapping("/deletePavingProcess")
	@ResponseBody
	public Result deletePavingProcess(PavingProcessModel processModel){
		Result res = new Result();
		UserModel obj = SessionMap.getObj();
		
		if(obj == null || Validata.isNullOrEmpty(obj.getUserId())){
			res.setCode(Result.CODE_LOGIN_REQUIRE);
			res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
			return res;
		}
		try {
			processModel.setUpdateUser(obj.getUserId());
			pavingProcessService.deletePavingProcess(processModel);
			res.setCode(Result.CODE_OK);
			res.setMsg(I18nResMsg.DELSUC());
		} catch (Exception e) {
			res.setCode(Result.CODE_ERROR);
			res.setMsg(I18nResMsg.DELERR());
			log.error(I18nResMsg.DELERR(), e);
		}
		return res;
	}
	@RequestMapping("/updatePavingProcessModel")
	@ResponseBody
	public Result updatePavingProcessModel(PavingProcessModel processModel){
		Result res = new Result();
		UserModel obj = SessionMap.getObj();
		
		if(obj == null || Validata.isNullOrEmpty(obj.getUserId())){
			res.setCode(Result.CODE_LOGIN_REQUIRE);
			res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
			return res;
		}
		if(id.equals(processModel.getPageTokenId())){
			res.setCode(Result.CODE_OK);
			return res;
		}
		try {
			processModel.setUpdateUser(obj.getUserId());
			id = processModel.getPageTokenId();
			pavingProcessService.updatePavingProcessModel(processModel);
			res.setCode(Result.CODE_OK);
			res.setMsg(I18nResMsg.UPDSUC());
		} catch (Exception e) {
			res.setCode(Result.CODE_ERROR);
			res.setMsg(I18nResMsg.UPDERR());
			log.error(I18nResMsg.UPDERR(), e);
		}
		return res;
	}
	@RequestMapping("/queryPavingProcessByPage")
	@ResponseBody
	public Result queryPavingProcessByPage(PavingProcessModel processModel){
		Result res = new Result();
		try {
			DataGrid<PavingProcessModel> queryPavingProcessByPage = pavingProcessService.queryPavingProcessByPage(processModel);
			res.setCode(Result.CODE_OK);
			res.setData(queryPavingProcessByPage);
		} catch (Exception e) {
			res.setCode(Result.CODE_ERROR);
			res.setMsg(I18nResMsg.EXECERR());
			log.error(I18nResMsg.EXECERR(), e);
		}
		return res;
	}
	
}
